package com.block.common.util;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

public class PwdUtil {

    /**
     * ".*[a-zA-Z]+.*"
     * ".*[a-z]+.*"
     */
    private static final String REG_PWD_LETTERS = "^[a-zA-Z]";
    /**
     * ".*\\d+.*"
     */
    private static final String REG_PWD_NUMBERS = "^[0-9]";
    /**
     * ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*"
     */
    private static final String REG_PWD_SPECIAL= "^[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]";

    /**
     * 验证密码是否符合规则
     * 1.至少8位
     * 2.包含字母
     * 3.包含数字或者特殊字符
     * @param pwdWord
     * @return
     */
    public static boolean checkPwd(String  pwdWord){
        if (StrUtil.length(pwdWord) < 8){
            return false;
        }
        boolean letterFlag = false;
        boolean numberOrSpecialFlag = false;
        for (int index = 0; index < pwdWord.length(); index++){
            char ch = pwdWord.charAt(index);
//            System.out.print(String.valueOf(ch) + " ");
            if (ReUtil.contains(REG_PWD_LETTERS, String.valueOf(ch))){
                letterFlag = true;
            }
            if (ReUtil.contains(REG_PWD_NUMBERS, String.valueOf(ch)) || ReUtil.contains(REG_PWD_SPECIAL, String.valueOf(ch))){
                numberOrSpecialFlag = true;
            }
        }
        return letterFlag && numberOrSpecialFlag;
    }

    public static void main(String[] args) {

        String pwdWord = "               ";
        pwdWord += "1231";
//        pwdWord += "asda";
        pwdWord += "ASD";
        pwdWord += "@&";

        System.out.println(pwdWord);
        System.out.println();
        System.out.println(checkPwd(pwdWord));

    }
}
